import { wxRequest } from "../../utils/httpRequest"
import initComputed from 'wx-computed'

const app = getApp()

Page({
    data: {
        shopId: '',
        inputSearchValue: '',
        tabIndex: 0,
        show: false,
        mealList: [],
        commentList: [],
        tagList: [],
        shopInfo: null,
        scrollId: "picture",
        orderId: null     // 如果之前生成订单但未支付,则其为上次订单的状态
    },
    getShopMeals() {
        let params = {
            shopId: this.data.shopId
        }
        wxRequest('getShopMeals', params).then((res) => {
            let is = res.data.meals.map((item) => {
              item.num = 0;
              item.ifShow = true;
              return item;
            });
            this.setData({ mealList: is.filter(item => item.grounding === 1) });
            this.setData({ shopInfo: res.data.shop});
            this.setData({ tagList: res.data.tags });
            this.setData({ commentList: res.data.comments })
            this.getUnPerfectedOrder();
        })
    },
    getUnPerfectedOrder() {
      let url = "getUnPerfectedOrder";
      let params = {
        shopId: this.data.shopId
      }
      wxRequest(url, params).then((res) => {
        if(res.data != "无订单") {
          this.setData({ orderId: res.data.orderId });
          let orderItems = res.data.orderItems;
          let list1 = this.data.mealList.map((item) => {
            let ls = orderItems.filter((itm) => item.mealId == itm.mealId);
            if(ls.length != 0) {
              item.num = ls[0].num;
            }
            return item;
          });
          this.setData({ mealList: list1 });
        }
      });
    },
    showpop() {
        this.setData({ showpop: true });
    },
    popOnClose() {
        this.setData({ showpop: false });
    },
    onChangeSiderBar(event) {
        if(event.detail == 0) {
            this.setData({scrollId: "picture"});
        } else {
            this.setData({scrollId: "nopicture"});
        }
    },
    bindscrollmeal(event) {
        this.getEleHeight("#nopicture")
    },
    getEleHeight(demo) {
        return new Promise((resolve, reject) => {
          wx.createSelectorQuery().select(demo).boundingClientRect((rect) => {
            if(rect){
                if(rect.top < 175) {
                    this.setData({tabIndex: 1});
                } else {
                    this.setData({tabIndex: 0});
                }
                resolve(rect)
            }else {
                reject(demo+'获取不到id rect的值为'+rect)
            }
          }).exec()
        })
    },
    changeMealNum(event) {
      let mealId = event.currentTarget.dataset.mealid;
      if(event.currentTarget.dataset.as == "add") {
        let is = this.data.mealList.map(item => {
          if(item.mealId == mealId) {
            this.setData({
              [item.num]: item.num++
            });
          }
          return item;
        });
        this.setData({ mealList: is});
      } else {
        let is = this.data.mealList.map(item => {
          if(item.mealId == mealId) {
            this.setData({
              [item.num]: item.num--
            });
          }
          return item;
        });
        this.setData({ mealList: is});
      }
    },
    filterMeals(event) {
      if(event.detail.value == "") {
        let is = this.data.mealList.map(item => {
            item.ifShow = true;
            return item;
        });
        this.setData({mealList:is});
      } else {
        let is = this.data.mealList.map(item => {
          if(item.mealName.indexOf(event.detail.value) != -1){
            item.ifShow = true;
          } else{
            item.ifShow = false;
          }
          return item;
        });
        this.setData({mealList:is});
      }
    },
    showAllMeals() {
      let is = this.data.mealList.map(item => {
        item.ifShow = true;
        return item;
      });
      this.setData({mealList:is});
    },
    goConfirmOrder() {
        let list = this.data.mealList.filter(item => item.num != 0);
        let list1 = list.map((item) => {
          item.allMealPrice = item.num * item.mealPrice;
          return item;
        });
        let obj = {
            orderId: this.data.orderId ? this.data.orderId : null,
            shopId: this.data.shopId,
            allPay: this.data.allMoney,
            orderItems: list1
        }
        let url = "submitOrder";
        wxRequest(url, obj).then((res) => {
          if (res.code == 10000) {
            let orderId = res.data;
            wx.navigateTo({
              url: `/home_package/confirmOrder/confirmOrder?id=${orderId}&shopName=${this.data.shopInfo.shopName}&shopId=${this.data.shopId}`,
            })
          }
        });

    },
    computed: {
      allMoney() {
        let flagList = this.data.mealList.filter(item => item.num != 0);
        let allMoney = 0;
        flagList.forEach(item => {
            allMoney = allMoney + item.num * item.mealPrice;
        })
        return allMoney
      },
      ifCanOrder() {
        let flagList = this.data.mealList.filter(item => item.num != 0);
        return flagList.length != 0;
      },
      ifShowTab() {
        let flagList = this.data.mealList.filter(item => !item.ifShow);
        return flagList.length == 0;
      },
      ifHasShow() {
        let flagList = this.data.mealList.filter(item => item.ifShow);
        return flagList.length == 0;
      }
    },
    onLoad(options) {
        initComputed(this)
        // this.setData({ shopId: "194269868" })
        this.setData({ shopId: options.shopId });
    },
    onShow() {
      this.getShopMeals();
    }
})